package com.dream52.pms.base;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dream52.pms.sys.entity.SysUser;
import com.dream52.pms.sys.service.SysUserService;
import com.dream52.pms.util.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class BaseController {






	@Autowired
	protected RedisUtil redis;


	// 1.数据集
	protected Map<String, Object> result;
	// 返回页面成功码
	protected final Integer SUCCESS_CODE = 200;
	// 返回页面失败码
	protected final Integer ERROR_CODE = 500;
	private final String SUCCESS_STR = "success";
	private final String ERROR_STR = "error";
	private final Integer PAGE_NO = 0;
	private final Integer PAGE_SIZE = 16;
	protected Integer pageNo = PAGE_NO;
	protected Integer pageSize = PAGE_SIZE;

	protected HttpServletResponse response;
	protected HttpServletRequest request;


	@ModelAttribute
	public void resetResult(HttpServletRequest request, HttpServletResponse response) {
		this.result.clear();
		if (null != request.getParameter("pageNo")) {
			String pageNoStr = request.getParameter("pageNo");
			this.pageNo = Integer.valueOf(pageNoStr) - 1;
		} else {
			this.pageNo = PAGE_NO;
		}
		if (null != request.getParameter("pageSize")) {
			String pageSizeStr = request.getParameter("pageSize");
			this.pageSize = Integer.valueOf(pageSizeStr);
		} else {
			this.pageSize = PAGE_SIZE;
		}

		this.response = response;
		this.request = request;
	}

	public BaseController() {
		if (null == result) {
			result = new HashMap<String, Object>();
		}
	}

	/**
	 * 解码
	 * 
	 * @param base64str
	 * @return
	 */
	private String decoder(String base64str) {
		try {
			Base64.Decoder decoder = Base64.getDecoder();
			String str = new String(decoder.decode(base64str), "UTF-8");
			return str;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param retCode
	 * @return retcode,retmsg,result
	 */
	protected String toPageJson(int retCode) {
		if (retCode == SUCCESS_CODE) {
			return toPageJson(SUCCESS_CODE, SUCCESS_STR);
		}
		return toPageJson(ERROR_CODE, ERROR_STR);
	}

	/**
	 * @param retCode
	 * @param retMsg
	 * @return retcode,retmsg,result
	 */
	protected String toPageJson(int retCode, String retMsg) {
		if (null == retMsg) {
			retMsg = "";
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("retcode", retCode);
		map.put("retmsg", retMsg);
		map.put("result", result);

		return JSONObject.toJSONString(map).toString();
	}

	/**
	 * PC管理后台登录用户token
	 * 
	 * @return
	 * @throws Exception
	 */
	protected SysUser getUserSession() throws Exception {
		String token = request.getHeader("token");

		if (null == token || "".equals(token)) {
			// 部分旧页面从请求拿token
			token = request.getParameter("token");
			if (null == token || "".equals(token)) {
				throw new PmsException(PmsConstant.ERROR_CODE_5005003,
						PmsConstant.ERROR_MAP.get(PmsConstant.ERROR_CODE_5005003));
			}
		}

		String redisStr = (String) redis.get(RedisConstant.PMS_LOG + token);
		if (StringUtils.isNotBlank(redisStr)) {
			SysUser user = JSON.parseObject(redisStr, SysUser.class);
			// SysUser user = this.UserService.getUserRedis(id);
			redis.set(RedisConstant.PMS_LOG + token, user, RedisConstant.OVERTIME);
			return user;
		} else {
			throw new PmsException(PmsConstant.ERROR_CODE_5005003,
					PmsConstant.ERROR_MAP.get(PmsConstant.ERROR_CODE_5005003));
		}


	}



	private static boolean isBase64(String str) {
		String base64Pattern = "^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$";
		return Pattern.matches(base64Pattern, str);
	}
}